﻿#include "MainWindow.h"
#include "AboutDialog.h"

#include <QDebug>

namespace Core {

	MainWindow::MainWindow(QWidget *parent)
		: QMainWindow(parent)
		, m_pSerialPortWidget(NULL)
	{
		ui.setupUi(this);
		this->setWindowTitle(u8"串口调试工具");

		connect(MessDispath::instance(), &MessDispath::signalSystemLogInfo, this, &MainWindow::slotSystemLogInfo);
		connect(MessDispath::instance(), &MessDispath::signalSerialPortSendMsgLength, this, &MainWindow::sloterialPortSendMsgLength);
		connect(MessDispath::instance(), &MessDispath::signalSerialPortRecvMsgLength, this, &MainWindow::slotSerialPortRecvMsgLength);
	}

	MainWindow::~MainWindow()
	{

	}

	void MainWindow::initAll()
	{
		initWidget();
		initMenuBar();
		initToolBar();
		initStatusBar();
	}

	void MainWindow::slotSystemLogInfo(QString strLog, int iLogType)
	{
		if (strLog.isEmpty())
		{
			return;
		}

		m_pStateLog->setText(strLog);

		QPalette pe;
		if (iLogType == 0)
		{
			pe.setColor(QPalette::WindowText, Qt::green);
		}
		else if(iLogType == -1)
		{
			pe.setColor(QPalette::WindowText, Qt::red);
		}
		m_pStateLog->setPalette(pe);
	}

	void MainWindow::sloterialPortSendMsgLength(int iLength)
	{
		m_pSendLog->setText(QString(u8"Rx：%1 Bytes").arg(iLength));
	}

	void MainWindow::slotSerialPortRecvMsgLength(int iLength)
	{
		m_pReavLog->setText(QString(u8"Tx：%1 Bytes").arg(iLength));
	}

	void MainWindow::slotAboutActionTriggered()
	{
		AboutDialog dialog(this);
		dialog.exec();
	}

	void MainWindow::initWidget()
	{
		m_pSerialPortWidget = new SerialPortWidget(this);
		this->setCentralWidget(m_pSerialPortWidget);
	}

	void MainWindow::initMenuBar()
	{
		m_pEditMenu = new QMenu(u8"编辑", this);
		{
			m_pActionOpen = new QAction(u8"打开", this);
			m_pActionClose = new QAction(u8"关闭", this);

			m_pActionClearLog = new QAction(u8"清除", this);
			m_pActionClearSendLog = new QAction(u8"清除发送", this);

			m_pEditMenu->addAction(m_pActionOpen);
			m_pEditMenu->addAction(m_pActionClose);
			m_pEditMenu->addSeparator();
			m_pEditMenu->addAction(m_pActionClearLog);
			m_pEditMenu->addAction(m_pActionClearSendLog);
		}
		ui.menuBar->addMenu(m_pEditMenu);

		m_pToolMenu = new QMenu(u8"工具", this);
		{
			m_pActionASCIICode = new QAction(u8"ASCII Code 表", this);
			m_pActionCrc = new QAction(u8"CRC 计算", this);

			m_pToolMenu->addAction(m_pActionASCIICode);
			m_pToolMenu->addAction(m_pActionCrc);
		}
		ui.menuBar->addMenu(m_pToolMenu);

		m_pHelpMenu = new QMenu(u8"帮助", this);
		{
			m_pActionAbout = new QAction(u8"关于", this);
			connect(m_pActionAbout, &QAction::triggered, this, &MainWindow::slotAboutActionTriggered);

			m_pHelpMenu->addAction(m_pActionAbout);
		}
		ui.menuBar->addMenu(m_pHelpMenu);
	}

	void MainWindow::initToolBar()
	{

	}

	void MainWindow::initStatusBar()
	{
		m_pStateLog = new QLabel(this);

		m_pSendLog = new QLabel(this);
		m_pSendLog->setText(u8"Rx：0 Bytes");

		m_pReavLog = new QLabel(this);
		m_pReavLog->setText(u8"Tx：0 Bytes");

		ui.statusBar->addWidget(m_pStateLog, 1);
		ui.statusBar->addWidget(m_pSendLog, 1);
		ui.statusBar->addWidget(m_pReavLog, 1);
	}

}//namespace Core